#!/usr/bin/python
#Drew Carpenter & Jeff Gullett
#CS 4444
#Excavare 0.0
#March 28, 2011
#this file requires graphics magick to be installed and useable from the command line
import os
import create_tabs
from pyPdf import PdfFileWriter, PdfFileReader
from drawable import *
	#here is how to load an image up
	#self.image = builder.get_object("fileimage")
	#self.image.clear()
	#self.image.set_from_file("Hello.pdf")
	
	#here is how to get text from a pdf
#output = PdfFileWriter()
def readPDF(newTab,filename,self):
	input1 = PdfFileReader(file(filename, "rb"))
	#print input1
	#print "title = %s" % (input1.getDocumentInfo().title)
	#print "document1.pdf has %s pages." % input1.getNumPages()
	num_pages=input1.getNumPages()
	page = input1.getPage(0)
	textpdf = page.extractText()
	if textpdf != "":
		newScrolledWindow,newTextView=create_tabs.create_scrolled_window()
		#get text buffer
		buffer = newTextView.get_buffer()						
		#call pdf opening routine
		self.notebook.append_page(newScrolledWindow,newTab)
		#put new textview in list of textviews
		self.textviews.append(newTextView)
		if num_pages>1:
			for i in range(1,num_pages):
				page = input1.getPage(i)
				#print page.extractText()
				textpdf=textpdf+"\n"+page.extractText()
		buffer.set_text(textpdf)
		buffer.get_end_iter()
		return num_pages
	else:
		path = "\""+filename
		temp = list(path)
		temp[-3]="t"
		temp[-2]="i"
		temp[-1]="f"
		dest = "".join(temp)
		#command = "convert "+path+dest
		if num_pages is 1:
			command = "gm convert -density 400 "+path+"\" -scale 2000x1000 "+dest+"\""
			os.system(command)
			newDestList = dest.split("\"")
			newDest = newDestList[1]
			image_view_event_box(self,newDest,True)
			return num_pages
		else:
			temp_dest=dest.split(".")
			dest = temp_dest[0]
			for i in range(0,num_pages):
				command = "gm convert -density 400 "+path+"["+str(i)+"]\" -scale 2000x1000 "+dest+str(i)+".tif\""
				if i is 0:
					os.system(command)
					newDestList = dest.split("\"")
					newDest = newDestList[1]
					newDest=newDest+str(i)+".tif"
					image_view_event_box(self,newDest,True)
				else:
					os.system(command)
			return num_pages
